// 省钱控页面js

// main 部分
let lastPage = document.querySelector('#lastpage')
let nextPage = document.querySelector('#nextpage')
let select = document.querySelector('select');
let option = document.querySelector('option')

if (select.selectedIndex === 0) {
    lastPage.disabled = true
}
//实现点击上一页跳转页面
let page = 0;//要先渲染页面，传入page=0，再进行点击页面，要不然页面时空白的
render(0)
lastPage.onclick = function () {
    html = "";
    page--;
    select.selectedIndex--;
   
    if (select.selectedIndex === 0) {
        lastPage.disabled = true
    } else if (select.selectedIndex !== 13) {
        nextPage.disabled = false
    }
    // console.log(page);
    // option.innerText = page / 14
    // console.log(option.innerText);  
    render(page)
}
nextPage.onclick = function () {//点击下一页获取数据跳转页面
    html = "";
    page++;
    select.selectedIndex++;
    if (select.selectedIndex === 0) {
        lastPage.disabled = true
    } else if (select.selectedIndex === 13) {
        nextPage.disabled = true
    }
    render(page)
}



function render(page) {//发送请求，获取数据把参数传进去
    ajax({
        url: `http://chst.vip:1234/api/getmoneyctrl?pageid=${page}`,
        // data: { pageid: page }

    })
        .then(res => {
            let html = "";
            let main = document.querySelector("main");
            // console.log(res);
            let resArr = res.result;
            // console.log(res.result);
            resArr.forEach(item => {
                html += `
          <div>
            <a  id="prevent">${item.productImgSm}</a>
            <ul>
                <li>
                    <a href="m-shangpinxiangqing.html?productId=${item.productId}">${item.productName}</a>
                    <span>${item.productPinkage}</span>
                </li>
                <li>
                    <span>${item.productFrom} | ${item.productTime} &nbsp;&nbsp;  ${item.productComCount}</span>
                </li>
            </ul>
        </div>
        `
            })
            main.innerHTML = html
        })
    // let prevent = document.querySelector('#prevent')  阻止默认事件
    // prevent.onclick = function (e) {
    //     e.preventDefault()
    // }


}

//实现点击选项框跳转指定页面
select.onchange = function () {
    // console.log(this.selectedIndex);
    render(this.selectedIndex)
    if (select.selectedIndex === 0) {
        lastPage.disabled = true
    }
}







// function render(currentArr) {

//     currentArr.forEach(item => {

//     });
//     return html
// }

// let bottom = document.querySelector("#bottom")
// let firstChild1 = bottom.firstChild;
// let lastChild1 = bottom.lastChild;

// firstChild1.onclick = function () {
//     window.history.back(-1);
// }
// lastChild1.onclick = function () {
//     window.history.back(+1);
// }

// window.onload = function () {
//     let prevent = document.querySelector('#prevent')
//     prevent.onclick = function (e) {
//         e.preventDefault()
    // }
